//index.js
// 语言包
var chi = require('../../utils/Chinese.js')
var eng = require("../../utils/English.js")
//获取应用实例
const app = getApp()
Page({
  data: {
    language:'',
    // 语言包对象
    content: '',
    // banner轮播图
    movies: [{id:"0", url: '../../img/banner_02.jpg' }, {id:"1", url: '../../img/banner_03.jpg' },{id:"2", url: '../../img/banner_04.jpg' }],
    inputValue: '',
    // 推荐系列轮播图
    pushs:[{id:'0',url:'../../img/sweiper_03.jpg',text:'「 沙司 」系列'},{id:'1',url:'../../img/sweiper_04.jpg',text:'「 果泥 」系列'},{id:'2',url:'../../img/sweiper_05.jpg',text:'「 分子 」系列'}],
    // 推荐产品
    product:{id:'1',url:'../../img/tui_03.jpg',name:'焦糖味糖浆',price:'107',describe:'法布芮Fabbri糖浆系列品种多元，口味丰润，果香浓郁，是调制鸡尾酒、风味饮料和特色糕点理想食材。'},
    pushs_all:[{id:'0',url:'../../img/tui_03.jpg',name:'焦糖味糖浆',price:'107'},{id:'1',url:'../../img/tui_03.jpg',name:'焦糖味糖浆',price:'107'},{id:'2',url:'../../img/tui_03.jpg',name:'焦糖味糖浆',price:'107'}],
  },

 // 页面函数 ========================================
  // 语言切换
  language:function(){
    let that = this
    var lag = that.data.language
    if(lag == 'zh_EN'){
      that.setData({
        language:'zh_CN',
        content: chi.content,
      })
      app.data.city= '全国'
      app.data.language = 'zh_CN'
    }else if(lag == 'zh_CN'){
      that.setData({
        language:'zh_EN',
        content: eng.content
      })
      
      app.data.city= 'Whole Country'
      app.data.language = 'zh_EN'
      app.data.my_city = ''
    }
    console.log(app.data.language)

    // 点击切换语言
    var lastLanuage = that.data.language

    console.log(lastLanuage)
    if(lastLanuage == 'zh_CN'){
      wx.setNavigationBarTitle({
        title: '法布芮官网精品商城' 
      })
      // 修改tabbar
      wx.setTabBarItem({
        index: 0,
        text: '首页'
      })
      wx.setTabBarItem({
        index: 1,
        text: '购物车',
      })
      wx.setTabBarItem({
        index: 4,
        text: '我的',
      })
    }else if(lastLanuage == 'zh_EN'){
      // 修改tabbar
      wx.setTabBarItem({
        index: 0,
        text: 'Home'
      })
      wx.setTabBarItem({
        index: 1,
        text: 'shopping'
      })
      wx.setTabBarItem({
        index: 4,
        text: 'My',
      })
      // 修改导航标题
      wx.setNavigationBarTitle({
        title: 'FABBRI Shopping Mall' 
      })
    }
   
  },
  // 搜索事件
  search:function(){
    var txt = this.data.inputValue
    console.log(txt)
  },
  bindKeyInput: function (e) {
    this.setData({
      inputValue: e.detail.value
    })
  },
 
  // 页面跳转
  all:function(){
    wx.navigateTo({
      url: '../products_all/products_all',
    })
  },
  world:function(){
    wx.switchTab({
      url: '../world/world',
    })
  },
  where:function(){
    wx.switchTab({
      url: '../where/where',
    })
  },
  shopping:function(){
    console.log('123')
    wx.reLaunch({
      url: '../shopping_cart/shopping_cart'
    })
  },
  product_all:function(){
    wx.navigateTo({
      url: '../products_all/products_all',
    })
  },
  // banner轮播
  toDetail:function(e){
      console.log(e.currentTarget.id)
  },
  // 推荐系列
  toPush:function(e){
    console.log(e.currentTarget.dataset.id)
  },
  product_top:function(e){
    // 跳转商品详情
    console.log(e.currentTarget.id)
    wx.navigateTo({
      url: '../comm/comm',
    })
  },
  toPushs_all:function(e){
    console.log(e.currentTarget.dataset.id)
  },

// 页面函数END=======================================================
  //事件处理函数

  onLoad: function () {
      let that = this
      this.setData({
        language:app.data.language
      })
      var lastLanuage = that.data.language
      if(lastLanuage == 'zh_CN'){
        that.setData({
          content: chi.content
        })
        wx.setNavigationBarTitle({
          title: '法布芮官网精品商城' 
        })
        // 修改tabbar
        wx.setTabBarItem({
          index: 0,
          text: '首页'
        })
        wx.setTabBarItem({
          index: 1,
          text: '购物车',
        })
        wx.setTabBarItem({
          index: 4,
          text: '我的',
        })
      }else if(lastLanuage == 'zh_EN'){
        that.setData({
          content: eng.content
        })
        // 修改tabbar
        wx.setTabBarItem({
          index: 0,
          text: 'Home'
        })
        wx.setTabBarItem({
          index: 1,
          text: 'shopping'
        })
        wx.setTabBarItem({
          index: 4,
          text: 'My',
        })
        // 修改导航标题
        wx.setNavigationBarTitle({
          title: 'FABBRI Shopping Mall' 
        })
      }

    // -------------------------------
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
